﻿using HZ.Public;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfAppDemo
{
    /// <summary>
    /// UserControl4.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl4 : UserControl
    {
        public UserControl4()
        {
            InitializeComponent();
            datagrid.ItemsSource = RuleData.RuleList;
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            UserControl2 uc = new UserControl2();
            //Window5 uc = new Window5();
            //PublicClass.ShowUCWin(uc, PublicClass.GetLanguage("新增策略"), null, null, null, true);//Could not load file or assembly 'NPOI, Version=2.1.3.1, Culture=neutral, PublicKeyToken=0df73ec7942b34e1'
            Window5 window5 = new Window5();
            window5.Show();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            //if (PublicClass.ShowMsgBox(PublicClass.GetLanguage("将会删除掉被勾选的策略，是否继续？")) == 1)//同上
            if (MessageBox.Show("将会删除掉被勾选的策略，是否继续？","提示",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                int n = datagrid.SelectedItems.Count;
                for (int i = n - 1; i >= 0; i--)
                {
                    RuleData.RuleList.Remove(datagrid.SelectedItems[i] as RuleProtoclClass);
                }
            }
        }
        private void btnRun_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnBackup_Click(object sender, RoutedEventArgs e)
        {
            UserControl3 uc = new UserControl3();
            //PublicClass.ShowUCWin_CanResize(uc, PublicClass.GetLanguage("自动备份"), 800, 600, null, null, null, true);//同上
            Window6 window6 = new Window6();
            window6.Show();
        }

        private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (datagrid.SelectedIndex != -1)
            {            
                UserControl2 uc = new UserControl2(RuleData.RuleList[datagrid.SelectedIndex], datagrid.SelectedIndex);
                PublicClass.ShowUCWin(uc, PublicClass.GetLanguage("编辑策略"), null, null, null, true);//如何动态加载用户控件到窗口
                //Window5 window5 = new Window5();
                //window5.Show();
            }
        }

        public CloseWinType DoBeforeCloseWin()
        {
            PublicClass.SaveSerializeObject(PublicClass.InfoDir + "RuleProtocol.json", RuleData.RuleList);
            return CloseWinType.Close;
        }

        public void Window_Close(object sender, System.ComponentModel.CancelEventArgs e)
        {
            PublicClass.SaveSerializeObject(PublicClass.InfoDir + "RuleProtocol.json", RuleData.RuleList);
            e.Cancel = false;
        }

        private void IsRun_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as CheckBox).IsChecked == true)
            {
                foreach (var item in RuleData.RuleList)
                {
                    if (item.IsRun == true && item.ActType == RuleData.RuleList[datagrid.SelectedIndex].ActType)
                    {
                        item.IsRun = false;
                    }
                }
                RuleData.RuleList[datagrid.SelectedIndex].IsRun = true;
            }
        }
    }
}
